package com.yunpuvip.sachet.modular.util.redis;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.nosql.redis.RedisDS;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.yunpuvip.sachet.modular.constant.ApplicationConstant;
import com.yunpuvip.sachet.modular.core.exception.RestException;
import com.yunpuvip.sachet.modular.core.exception.RestServiceExceptionEnum;
import com.yunpuvip.sachet.modular.page.RedisPage;
import com.yunpuvip.sachet.modular.page.RestPageFactory;
import com.yunpuvip.sachet.modular.vo.rest.redis.GiveALikeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.util.*;

/**
 * @author: Lincoln
 * @Date: 2019/4/25 16:19
 * @Description:
 */
@Slf4j
//@Component
public class RedisHelper {

    static Jedis jedis;


    private ApplicationContext applicationContext ;


    public RedisHelper(ApplicationContext applicationContext) {
        if (jedis == null){
            this.applicationContext = applicationContext;
            String acticeProfile = applicationContext.getEnvironment().getActiveProfiles()[0];
            jedis = RedisDS.create(acticeProfile).getJedis();
        }
    }


    //释放链接
    public static synchronized void returnToPool(Jedis jedis) {
        try {
            if (jedis != null) {
                jedis.close();//替代方法
                log.info("returnToPool success ");
            }
        } catch (Exception e) {
            log.error("returnToPool error " + e.toString());
        }
    }

}
